package io.github.uicdb.et.block;

import com.mojang.logging.LogUtils;

import io.github.uicdb.et.ModBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;


import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
import net.minecraft.world.level.block.state.pattern.BlockPatternBuilder;
import net.minecraft.world.level.block.state.predicate.BlockPredicate;
import net.minecraft.world.phys.BlockHitResult;


public class AvatorBlock extends Block {
    public AvatorBlock(Properties properties) {
        super(properties);
    }

    @Override
    protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
        var LOGGER = LogUtils.getLogger();
        if (level.isClientSide() || hand == InteractionHand.OFF_HAND) return ItemInteractionResult.SUCCESS;
        var pattern = BlockPatternBuilder.start()
                .aisle("   ", " b ", "   ")
                .aisle(" c ", "cac", " c ")
                .where('a', BlockInWorld.hasState(BlockPredicate.forBlock(Blocks.DIRT)))
                .where('b', BlockInWorld.hasState(s -> s.is(ModBlocks.AVATOR)))
                .where('c', BlockInWorld.hasState(BlockPredicate.forBlock(Blocks.STONE)))
                .build();
        var find = pattern.find(level, pos);

        LOGGER.debug("Pos:{},find:{}", pos, find);
        if (find != null) {
            for (int i = 0; i < pattern.getWidth(); i++) {
                for (int j = 0; j < pattern.getHeight(); j++) {
                    for (int k = 0; k < pattern.getDepth(); k++) {
                        var blockinworld = find.getBlock(i, j, k).getState();
                        var pos2 = find.getBlock(i, j, k).getPos();
                        LOGGER.debug("x={},y={},z={},BlockInWorld={}", pos2.getX(), pos2.getY(), pos2.getZ(), blockinworld);
                    }
                }
            }
        }
        return super.useItemOn(stack, state, level, pos, player, hand, hitResult);
    }

    @Override
    protected boolean skipRendering(BlockState state, BlockState adjacentState, Direction direction) {
        return super.skipRendering(state, adjacentState, direction);
    }
}
